package com.yequan.hbb.view.fragment.loginfragment;


import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.yequan.hbb.R;
import com.yequan.hbb.app.MyApplication;
import com.yequan.hbb.view.LoginActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class PasswordFragment extends Fragment implements View.OnClickListener {

    public final static int PASSWORD = 3;
    private TextInputEditText password;
    private Button complete;
    private static String userPhone;
    private String userPassword;
    private ContentResolver resolver;
    private boolean isExist = false;
    private Uri uri;
    private android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 100:
                    getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.login_container, LoginActivity.loginFragment).commit();
                    break;
            }
        }
    };

    public PasswordFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_password, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MyApplication.level = PASSWORD;
        password = (TextInputEditText) view.findViewById(R.id.completePassword);
        complete = (Button) view.findViewById(R.id.register_complete);
        password.setHint("设置一个最少6位的密码");
        complete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        userPassword = password.getText().toString().trim();
        if ("".equals(userPassword)) {
            password.setError("密码不能为空");
        } else if (userPassword.length() < 6) {
            password.setError("密码不能少于6位");
        } else {
            resolver = getContext().getContentResolver();
            uri = Uri.parse("content://com.yequan.hbbContentProvider/user");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Cursor cursor = resolver.query(uri, new String[]{"phone"}, null, null, null, null);
                    while (cursor.moveToNext()) {
                        int phoneIndex = cursor.getColumnIndex("phone");
                        String phone = cursor.getString(phoneIndex);
                        if (userPhone == phone) {
                            isExist = true;
                        }
                    }
                }
            }).start();
            if (!isExist) {
                MyApplication.handler = handler;
                final ContentValues values = new ContentValues();
                values.put("phone", userPhone);
                values.put("password", userPassword);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        resolver.insert(uri, values);
                    }
                }).start();
            } else {
                Toast.makeText(getContext(), "电话已注册,可以直接登录", Toast.LENGTH_SHORT).show();
                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.login_container, LoginActivity.loginFragment).commit();
            }
        }
    }

    public static class passwordPhoneCodeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            userPhone = getResultData();
            abortBroadcast();
        }
    }
}
